<template>
	<view>
		<transition-group name="barrage" class="barrage" :style="{bottom:bottom+'rpx',left:left+'rpx'}">
			<view class="barrage-item" v-for="(item,index) in barrageList" :key="item.id" :class="{barrageLeaveActive:item.id == endId,barrageEnterActive:item.id == endId}">
				<image class="barrage-image" :src="avatarImg(item.imageUrl)"></image>
				<text class="barrage-text"><text style="color:#FFD97A;margin: 0 10rpx;">{{item.name}}</text>{{item.remark}}</text>
			</view>
		</transition-group>
	</view>
</template>

<script>
	import config from '@/config'
	import { isHttp, isEmpty } from "@/utils/validate"
	import defAva from '@/static/logo.png'
	const baseUrl = config.baseUrl
	export default {
		name: "sinBarrage",
		props: {
			list: {
				type: Array || Object,
				default () {
					return {}
				}
			},
			rows: {
				type: Number,
				default: 3
			},
			color: {
				type: String,
				default: '#FFFFFF'
			},
			background: {
				type: String,
				default: '#000000'
			},
			opacity: {
				type: Number,
				default: 1
			},
			left: {
				type: Number,
				default: 35
			},
			bottom: {
				type: Number,
				default: 120
			},
			msec: {
				type: Number,
				default: 2000
			}
		},
		data() {
			return {
				barrageList: [],
				endId:0,
				objBFirstItem:0,
			}
		},
		methods:{
			avatarImg(item){
				let avatar = item || ""
				if (!isHttp(avatar)) {
				  avatar = (isEmpty(avatar)) ? defAva : baseUrl + avatar
				}
				return avatar;
			},
		},
		created() {
			var a = setInterval(() => {
				/** 此处逻辑：
				 * 设定A数组为展示数组（默认3条数据），B数组为源数组（n条数据）
				 * 首次进入页面，因A数组为空，所以根据rows设定每次从B数组头部取1条数据移入A数组尾部
				 * 此后，在每个定时周期内，把A数组头部第1条数据移出来，并移入B数组尾部
				 * 同时把B数组头部第1条数据移出来，并移入A数组尾部
				 * 如此循环即可
				 */
				if(!this.list || this.list.length == 0){
					//clearInterval(a)
					return
				}
				this.barrageList.push(this.list[0]);
				this.list.splice(0, 1);
				setTimeout(()=>{
					let objAFristItem = this.barrageList[0]
					this.endId = objAFristItem.id
					this.list.push(objAFristItem)
					setTimeout(()=>{
						this.barrageList.splice(objAFristItem, 1)
						this.endId = ""
					},this.msec/2)
				},this.msec*this.rows)
				// if (this.barrageList.length < this.rows) {
					
					
				// } else {
					
					
				// 	//clearInterval(a)
				// }
			}, this.msec)
		},
		watch: {

		}
	}
</script>

<style lang="scss">
	.barrage-item {
		//transition: all 1s;
		opacity: 1;
		animation: fadeIn 1s;
	}
	/* 淡入淡出start */
	@keyframes fadeIn {
		0% {
			opacity: 0;
		}
		100% {
			opacity: 1;
		}
	}
	/* 淡入淡出start */
	@keyframes fadeOut {
		0% {
			opacity: 1;
		}
		100% {
			opacity: 0;
		}
	}

	.barrage-enter {
		opacity: 1;
		transform: translateY(30px);
	}

	.barrage-leave-to {
		opacity: 0;
		transform: translateY(0px);
	}

	.barrageEnterActive {
		transition: all 1s;
		opacity: 1;
		//animation: fadeIn 6s ease;
	}

	.barrageLeaveActive {
		transition: all 1s;
		opacity: 0;
		// animation: fadeOut 1s;
	}

	.barrage {
		position: absolute;

		.barrage-item {
			margin-top: 10rpx;
			border-radius: 30rpx;
			background: #00000088;
			padding: 5rpx 20rpx 5rpx 5rpx;
			.barrage-image {
				display: inline-block;
				vertical-align: middle;
				width: 38rpx !important;
				height: 38rpx !important;
				margin-right: 10rpx;
				border-radius: 30rpx;
			}

			.barrage-text {
				font-size: 24rpx;
				line-height: 36rpx;
				vertical-align: middle;
				color: #FFFFFF;
			}
		}
	}
</style>
